'''
Created on 06/07/2010

@author: Mar
'''


class DoubleIndirectionBlock(object):
    
    
    def __init__(self):
        self.maxSize = 32 
        self.pointers = [] 
        
    def getPointers(self):
        return self.pointers
    
    def hasSpaceToABlock(self): 
        if(self.maxSize > len(self.pointers)):
            return True
        else:
            return False    
        
    def addIndirectionBlock(self, anIndirectionBlock):
        if(self.hasSpaceToABlock()):
            if(not(self.getPointers().__contains__(anIndirectionBlock))):
                self.pointers.append(anIndirectionBlock) 
    
    def removeIndirectionBlock(self, anIndirectionBlock):
        if(self.getPointers().__contains__(anIndirectionBlock)):
            self.getPointers().remove(anIndirectionBlock)        
            
            
    def hasSpace(self): 
        for i in self.pointers: 
            if i.hasSpace():
                return True
        return False 
           
    def getFreeSpace(self):
        #=======================================================================
        # Retorna el primer espacio libre encontrado en el bloque
        #=======================================================================
        for i in self.pointers:
            if i.hasSpace():
                return i  
    
    def addDataPointer1(self, aPointer, aCounter):
        if(self.hasSpace()):
            for i in self.pointers: 
                if (i.hasSpace()):  
                    i.addPointer(aPointer, aCounter)
                    break 
                
    def addDataPointer(self, aPointer, aCounter):
        if(self.hasSpace()): 
            space = self.getFreeSpace()
            space.addDataPointer(aPointer, aCounter)
            
    def removeDataPointer(self, aPointer):
        for i in self.getPointers():
            if i.getDataBlockPointers().__contains__(aPointer):
                i.removeDataPointer(aPointer)
                    
    def contains(self, aPointer):
        for i in self.getPointers():
            if i.contains(aPointer):
                return True
        return False
    
    def getBlocks(self):
        listOfBlocks = []
        for i in self.pointers:
            list = i.getBlocks()
            for j in list:
                listOfBlocks.append(j)
        return listOfBlocks
    